package com.example.h1nson.realphotography.viewpager;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.activity.PhotoDetailActivity;
import com.example.h1nson.realphotography.adapter.PushAdapter;
import com.example.h1nson.realphotography.asyncTask.PushAsynTask;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.interface_photo.OnPushItemListener;
import com.markupartist.android.widget.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainTab01 extends android.support.v4.app.Fragment implements BDLocationListener {
    public LocationClient mLocationClient = null;
    private LocationClientOption mOption;
    private List<Map> list = new ArrayList<>();
    private LayoutInflater inflater;
    private double longitude;
    private double latitude;
    private  PushAdapter pushAdapter;
    private PullToRefreshListView pullToRefreshListView;
    private OnPushItemListener onPushItemListener;
    public MainTab01() {
    }
    public void setOnPushItemListener(OnPushItemListener onPushItemListener){
        this.onPushItemListener = onPushItemListener;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        getDataTask();
        View view = inflater.inflate(R.layout.main_tab_01, container, false);
        pullToRefreshListView =(PullToRefreshListView)view.findViewById(R.id.listview_push);
        pushAdapter = new PushAdapter(inflater.getContext(),list);
        pullToRefreshListView.setAdapter(pushAdapter);
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map map = list.get(position-1);
                int photoId = (int)map.get("photoId");
                onPushItemListener.goPhotoDetail(photoId);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list = new ArrayList<>();
                getDataTask();
            }
        });
        pullToRefreshListView.setLoadMoreListen(new PullToRefreshListView.OnLoadMore() {
            @Override
            public void loadMore() {
                load();
            }
        });
        return view;
    }
    private void getDataTask(){
        mLocationClient = new LocationClient(inflater.getContext());
        mLocationClient.setLocOption(getDefaultLocationClientOption());
        mLocationClient.registerLocationListener(this);
        mLocationClient.start();
    }
    public LocationClientOption getDefaultLocationClientOption(){
        if(mOption == null){
            mOption = new LocationClientOption();
            mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
            mOption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
            mOption.setScanSpan(3000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
            mOption.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
            mOption.setIsNeedLocationDescribe(true);//可选，设置是否需要地址描述
            mOption.setNeedDeviceDirect(false);//可选，设置是否需要设备方向结果
            mOption.setLocationNotify(false);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
            mOption.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
            mOption.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
            mOption.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
            mOption.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        }
        return mOption;
    }
    private void load(){
        PushAsynTask pushAsynTask = new PushAsynTask(longitude,latitude,list.size());
        pushAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
            @Override
            public void onDataSuccessfully(Object data) {
                try {
                    JSONObject jsonObject = new JSONObject(data.toString());
                    int size =jsonObject.getInt("size");
                    for(int i=0;i<size;i++){
                        Map map = new HashMap();
                        map.put("photoId",jsonObject.getInt("photoId"+i));
                        map.put("photoUrl",jsonObject.getString("photoUrl"+i));
                        map.put("photoAddress",jsonObject.getString("photoAddress"+i));
                        map.put("photoDistance",jsonObject.getDouble("photoDistance"+i));
                        list.add(map);
                    }
                    System.out.println(list);
                    //pushAdapter.notifyDataSetChanged();
                    pullToRefreshListView.onRefreshComplete();
                    pullToRefreshListView.onLoadComplete();

                }catch (JSONException e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onDataFailed() {

            }
        });
        pushAsynTask.execute();
    }
    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        if(bdLocation!=null){
            longitude = bdLocation.getLongitude();
            latitude = bdLocation.getLatitude();
            load();
        }
        mLocationClient.stop();
    }
}